package service

import (
	"github.com/gin-gonic/gin"
	"micro-chat/config"
	"micro-chat/internal/model"
	"micro-chat/internal/request"
	"micro-chat/internal/response"
	"net/http"
	"strconv"
	"time"
)

func FindKeyFriend(c *gin.Context) {
	//s := c.GetString("user_id")
	//id, _ := strconv.Atoi(s)

	var req request.FindKeyFriendForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
		})
		return
	}

	var user model.User
	var list []*response.UserInfo

	key, err := user.FindUserByKey(req.Key)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查询失败",
			"data":    err.Error(),
		})
		return
	}

	for _, k := range key {
		info := &response.UserInfo{
			Nickname:  k.Nickname,
			Avatar:    k.Avatar,
			Signature: k.Signature,
			Region:    k.Region,
		}

		list = append(list, info)
	}

	config.RDB.Set(config.Ctx, "userinfo", list, time.Hour*3)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查询成功",
		"data":    list,
	})

}

func SendApply(c *gin.Context) {
	s := c.GetString("user_id")
	id, _ := strconv.Atoi(s)

	var req request.SendApplyForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
		})
		return
	}

	var apply model.FriendApply

	apply = model.FriendApply{
		FormUserId:  int32(id),
		ToUserId:    req.ToUserId,
		ApplyReason: req.ApplyReason,
	}
	err := apply.SendApply()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "申请发送失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "申请发送成功",
		"data":    apply.Id,
	})

}

func FindApply(c *gin.Context) {
	s := c.GetString("user_id")
	id, _ := strconv.Atoi(s)

	var apply model.FriendApply

	findApply, err := apply.FindApply(int32(id))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "申请查看失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "申请查看成功",
		"data":    findApply,
	})

}

func ApplyUpdate(c *gin.Context) {

	var req request.ApplyUpdateForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
		})
		return
	}

	var apply model.FriendApply
	apply.Id = req.Id
	apply.Status = req.Status
	err := apply.UpdateApply()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "申请处理失败",
			"data":    err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "申请处理成功",
		"data":    apply,
	})
}

func ListFriend(c *gin.Context) {
	s := c.GetString("user_id")
	id, _ := strconv.Atoi(s)

	var friend model.FriendRelation
	var user model.User
	var list []*response.FriendInfo

	listFriend, err := friend.ListFriend(int32(id))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查询失败",
			"data":    err.Error(),
		})
		return
	}
	for _, k := range listFriend {
		user.FindUser(uint(k.UserId))
		key := &response.FriendInfo{
			Nickname:      user.Nickname,
			Avatar:        user.Avatar,
			Signature:     user.Signature,
			LastLoginTime: user.LastLoginTime,
		}
		list = append(list, key)
	}

	config.RDB.Set(config.Ctx, "FriendList", list, time.Hour*3)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "好友列表查看成功",
		"data":    list,
	})

}
